package com.lidong.controller.product;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lidong.entity.Product;
import com.lidong.exception.ProductNotFoundException;
import com.lidong.mapper.ProductMapper;

/**
 * @Description : 
 * @Author : lidong
 * @Date : 2017年10月27日
 */
@RestController
@RequestMapping("/product")
public class ProductController {

	@Autowired
	ProductMapper productMapper;
	
	@GetMapping("/{id}")
	public Product getProductInfo(@PathVariable String id) {
		return productMapper.select(id);
	}
	
	@PutMapping("/{id}")
	public Product updateProductInfo(@PathVariable String id, @RequestBody Product newProduct) {
		
		Product product = productMapper.select(id);
        if (product == null) {
			throw new ProductNotFoundException(id);
		}
        product.setName(newProduct.getName());
        product.setPrice(newProduct.getPrice());
        productMapper.update(product);
        return product;
	}
	
}
